﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
	/// <summary>
	/// 场景围栏配表脚本
	/// </summary>
	[ExecuteInEditMode]
	public class SceneFence : SceneObject
	{

#if UNITY_EDITOR
		[Header("fenceID")]
		[SerializeField]
		private int fenceID;

		[SerializeField]
		[Tooltip("The offset of the door view.")]
		private Vector3 offset;

		[SerializeField]
		[Tooltip("The rotation of the door view.")]
		private Vector3 rotation;

		[SerializeField]
		[Tooltip("The resource id of the fence view.")]
		private string resID = "men_dcq01";


		[Header("预览对象资源")]
		[SerializeField]
		private AssetID previewAssetId;

		// The preview prefab.
		private GameObject previewPrefab;

		public int ID { get { return this.fenceID; } }
		public Vector3 Offset {	get { return this.offset; }}
		public Vector3 Rotation { get { return this.rotation; } }
		public string ResID { get { return this.resID; } }

		protected override bool HasPreview()
		{
			return this.previewPrefab != null;
		}

		protected override GameObject BuildPreview()
		{
			var obj = Instantiate(this.previewPrefab);
			obj.transform.localPosition = this.offset;
			obj.transform.localRotation = Quaternion.Euler(this.rotation);
			return obj;
		}

		void OnValidate()
		{
			if (Application.isPlaying) return;

			this.name = "Fence" + this.fenceID;
			if (!string.IsNullOrEmpty(previewAssetId.AssetGUID))
			{
				this.previewPrefab = previewAssetId.LoadObject<GameObject>();
			}
			else
			{
				//Debug.LogWarning("Can not find NPC with id: " + this.npcId);
				this.previewPrefab = null;
			}

			if (this.PreviewObject != null)
			{
				this.PreviewObject.transform.localPosition =
					this.offset;
				this.PreviewObject.transform.localRotation =
					Quaternion.Euler(this.rotation);
			}

			this.RefreshPreview();
		}



#endif

	}
}

